package net.vinote.smart.platform.service.action;

import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

import net.vinote.smart.platform.comm.DateFunction;
import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.BeanEntityFactory;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.log.RunLogger;
import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.bean.TimerTask;
import net.vinote.smart.platform.service.util.AutoInjection;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;
import net.vinote.smart.platform.timer.TimerManager;
import net.vinote.smart.platform.timer.TimerTask.TimerTaskStatus;

import org.apache.commons.lang.math.NumberUtils;

public class TimerTaskAction {
	@AutoInjection
	private String id;

	/**
	 * 切换运行状态
	 * 
	 * @return
	 * @throws Exception
	 */
	@Permission(PermissionEnum.SMART_TIME_TASK_UPDATE)
	public String toggleStatus() throws Exception {
		if (TimerManager.getInstance().getTaskStatus(id) == TimerTaskStatus.SCHEDULED) {
			TimerManager.getInstance().cancleTask(id);
		} else {
			DBManager bm = new DBManager("timer_task");
			BizObject bo = bm
					.createQuery(SRE.getTable("timer_task").getFieldsName(),
							"id=? and isinuse=?")
					.setParamVal(id, PlatformConst.YES).getSingleResult();
			if (bo != null) {
				TimerTask task = new TimerTask();
				BeanEntityFactory.getInstance().getObject(task, bo);
				// 执行完毕的先将其取消
				if (TimerManager.getInstance().getTaskStatus(id) == TimerTaskStatus.EXECUTED) {
					TimerManager.getInstance().cancleTask(task.getId());
				}
				if (StringFunction.isBlank(task.getFinishTime())
						|| DateFunction.getTime(task.getFinishTime()) > System
								.currentTimeMillis()) {
					try {
						TimerManager.getInstance().schedule(
								task.getId(),
								DateFunction.getTime(task.getStartTime()),
								DateFunction.getTime(task.getFinishTime()),
								TimeUnit.valueOf(task.getPeriodType())
										.toMillis(
												NumberUtils.toLong(task
														.getPeriod())),
								task.getTriggerClass());
					} catch (Exception e) {
						RunLogger.getLogger().log(e);
						return TimerTaskStatus.EXCEPTION.getName();
					}
				}
			} else {
				return TimerTaskStatus.UNDEFINITION.getName();
			}

		}
		return getStatus();
	}

	@Permission(PermissionEnum.SMART_TIME_TASK_QUERY)
	public String getStatus() {
		TimerTaskStatus status = TimerManager.getInstance().getTaskStatus(id);
		if (status == null) {
			DBManager bm = new DBManager("timer_task");
			try {
				BizObject bo = bm
						.createQuery(
								SRE.getTable("timer_task").getFieldsName(),
								"id=? and isinuse=?")
						.setParamVal(id, PlatformConst.YES).getSingleResult();
				if (bo == null) {
					status = TimerTaskStatus.UNDEFINITION;
				} else {
					TimerTask task = new TimerTask();
					BeanEntityFactory.getInstance().getObject(task, bo);
					if (!StringFunction.isBlank(task.getFinishTime())
							&& DateFunction.getTime(task.getFinishTime()) < System
									.currentTimeMillis()) {
						status = TimerTaskStatus.DEPRECATED;
					} else {
						status = TimerTaskStatus.UNINITIALIZE;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
				status = TimerTaskStatus.EXCEPTION;
			}
		}
		return status.getName();
	}

	public void setId(String id) {
		this.id = id;
	}

}
